package com.computerManagementSystem.controller;

import com.computerManagementSystem.pojo.BorrowThing;
import com.computerManagementSystem.pojo.Things;
import com.computerManagementSystem.service.ThingServiceImpl;
import com.computerManagementSystem.service.ThingsService;
import com.computerManagementSystem.util.CookieUtil;
import com.computerManagementSystem.util.JWTUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 赖志宇
 * @date 2020/6/27 16:58
 */
@RestController
@RequestMapping(value = "member",method = RequestMethod.POST)
@Validated
public class ThingsController {

    @Resource(name = "ThingService")
    private ThingServiceImpl thingsService;

    @PostMapping("borrowThing")
    public void borrowThing(@RequestBody @Validated BorrowThing borrowThing, HttpServletRequest request){
        String token = CookieUtil.getCookieValueNamedToken(request);
        String id = JWTUtil.getClaimNamedAccount(token);
        borrowThing.setId(id);
        thingsService.borrowThing(borrowThing);
    }

    @PostMapping("returnThing")
    public void returnThing(@RequestBody BorrowThing borrowThing,HttpServletRequest httpServletRequest){
        String token = CookieUtil.getCookieValueNamedToken(httpServletRequest);
        String id = JWTUtil.getClaimNamedAccount(token);
        borrowThing.setId(id);
        thingsService.returnThing(borrowThing);
    }

    @PostMapping("addItem")
    public void addThing(@RequestBody @Validated Things things){
        thingsService.addNewThing(things);
    }

    @PostMapping("changeItemInfo")
    public void modifyThing(@RequestBody Things things){
        System.out.println("hello");
        thingsService.modifyThing(things);
    }

    @PostMapping("getWaitingReturnList")
    public Map<String,Object> getMemberSelfBorrowingThing(HttpServletRequest httpServletRequest){
        String token = CookieUtil.getCookieValueNamedToken(httpServletRequest);
        String id = JWTUtil.getClaimNamedAccount(token);
        List<BorrowThing> borrowThingList = thingsService.getMemberSelfBorrowingThingList(id);
        Map<String, Object> res= new HashMap<>();
        res.put("code", 0);
        res.put("count", borrowThingList.size());
        res.put("data", borrowThingList);
        res.put("msg", "");
        return res;
    }

    @PostMapping("getThingList")
    public Map<String,Object> getThingList(@RequestParam(value = "page")int page,@RequestParam(value = "limit") int limit){
       List<Things> thingsList = thingsService.getThingList(page,limit);
       int count = thingsService.getThingListNum();
        Map<String, Object> res= new HashMap<>();
        res.put("code", 0);
        res.put("count", count);
        res.put("data", thingsList);
        res.put("msg", "");
        return res;
    }

    @PostMapping("getCanBorrowList")
    public Map<String,Object> getThingCanBorrowList(@RequestParam(value = "page")int page,@RequestParam(value = "limit") int limit){
        List<Things> thingsList = thingsService.getCanBorrowThingList(page,limit);
        int count = thingsService.getCanBorrowNum();
        Map<String, Object> res= new HashMap<>();
        res.put("code", 0);
        res.put("count", count);
        res.put("data", thingsList);
        res.put("msg", "");
        return res;
    }

    @PostMapping("getBorrowList")
    public Map<String,Object> getBorrowList(@RequestParam(value = "page")int page,@RequestParam(value = "limit") int limit){
        List<BorrowThing> thingsList = thingsService.getMemberBorrowThingList(page, limit);
        int count = thingsService.getBorrowThingListCount();
        Map<String, Object> res= new HashMap<>();
        res.put("code", 0);
        res.put("count", count);
        res.put("data", thingsList);
        res.put("msg", "");
        return res;
    }

}
